package com.aioething.iot.controller;

import cn.hutool.core.util.IdUtil;
import com.aioething.iot.dto.FirmwareDto;
import com.aioething.iot.model.Firmware;
import com.aioething.iot.service.IFirmwareService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("firmware")
public class FirmwareController {
    @Autowired
    private IFirmwareService firmwareService;

    @PutMapping
    public int save(Firmware firmware, @RequestParam MultipartFile file) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        firmware.setFirmwareId(IdUtil.fastSimpleUUID());
        firmware.setCreateTime(new Date());
        return firmwareService.save(firmware, file);
    }

    @DeleteMapping
    public int delete(@RequestParam String firmwareId) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        return firmwareService.delete(firmwareId);
    }

    @GetMapping
    public PageInfo<Firmware> query(FirmwareDto firmwareDto) {
        return firmwareService.query(firmwareDto);
    }

    @GetMapping("list")
    public List<Firmware> list(FirmwareDto firmwareDto) {
        return firmwareService.list(firmwareDto);
    }
}
